package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.DiscussMapper;
import cn.wolfcode.trip.base.mapper.MycommentMapper;
import cn.wolfcode.trip.base.mapper.PersonalityMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.DiscussQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDiscussService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DiscussServiceImpl implements IDiscussService{

    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private PersonalityMapper personalityMapper;

    @Autowired
    private MycommentMapper mycommentMapper;

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {

    }

    @Override
    public void insert(Discuss discuss) {
        //敏感词过滤
        List<Personality> personalities = personalityMapper.selectAll();//得到数据库中存储的敏感词集合
        for (Personality personality : personalities) {//遍历
            //判断评论当中是否包含集合中的某个敏感词
            if( discuss.getDiscussContent().contains(personality.getWord())){

                StringBuffer str = new StringBuffer();
                for (int i = 0; i < personality.getWord().length(); i++) {//是的话就得到当前敏感词的长度
                   str.append("*");//遍历，拼接与长度相同的*代替敏感词
                }
                discuss.setDiscussContent(discuss.getDiscussContent().replace(personality.getWord(),str));
            }
        }
        discussMapper.insert(discuss);
        Article article = new Article();
        Mycomment mycomment = new Mycomment();
        mycomment.setComment(discuss.getDiscussContent());
        mycomment.setCommendtime(new Date());
        //得到大评论
        StrategyComment strategyComment = strategyCommentMapper.selectByPrimaryKey(discuss.getArticelId());
        //得到大攻略id
        Long strategyId = strategyComment.getStrategy().getId();
        String title = strategyComment.getStrategy().getTitle();
        article.setContent(title);
        article.setId(strategyId);
        mycomment.setArticle(article);
        mycomment.setTypestatus(2);
        User user = UserContext.getCurrentUser();
        mycomment.setUser(user);
        mycommentMapper.insert(mycomment);
    }

    @Override
    public Discuss selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public List<Discuss> selectAll() {
        return null;
    }

    @Override
    public PageInfo query(DiscussQuery qo) {
        //PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        //全部评论
        //一级评论
        List<Discuss> list = discussMapper.selectForlist(qo);

        //回复
        List<Discuss> discusses = discussMapper.selectTwolist(qo);
        List<Discuss> list2 = new ArrayList<>();//定义一个新集合
        for (Discuss discuss : list) {//遍历所有的一级评论
            ArrayList<Discuss> list1 = new ArrayList<>();
            for (Discuss reply : discusses) {//变量所有的回复
                if (discuss.getId()==reply.getParentId()){//如果一级评论的id等于回复的父id，添加到list1中中
                    list1.add(reply);
                    System.out.println(list1);
                }
            }
            discuss.setDiscusses(list1);//设置封装的discusses属性
            list2.add(discuss);
        }
       /*  List<Discuss> list3 = new ArrayList<>();
        for (Discuss discuss : list2) {
             if( discuss.getDiscussContent().contains("傻")){
                 discuss.setDiscussContent(discuss.getDiscussContent().replace("傻","*"));
             }
             list3.add(discuss);
        }*/
        return new PageInfo(list2);
    }
}
